/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.entity.FeeCharge;
import com.jiadakeji.ppm.modules.business.entity.FeeReceive;
import com.jiadakeji.ppm.modules.business.service.FeeChargeService;
import com.jiadakeji.ppm.modules.business.service.FeeReceiveService;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 实收台账Controller
 * @author HHAOYU
 * @version 2019-07-09
 */
@Controller
@RequestMapping(value = "${adminPath}/business/feeReceive")
public class FeeReceiveController extends BaseController {

	@Autowired
	private FeeReceiveService feeReceiveService;
	@Autowired
	private FeeChargeService feeChargeService;
	
	@ModelAttribute
	public FeeReceive get(@RequestParam(required=false) String id) {
		FeeReceive entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = feeReceiveService.get(id);
		}
		if (entity == null){
			entity = new FeeReceive();
		}
		return entity;
	}
	
	@RequiresPermissions("business:feeReceive:view")
	@RequestMapping(value = {"list", ""})
	public String list(FeeReceive feeReceive, HttpServletRequest request, HttpServletResponse response, Model model) {
		feeReceive.setStatus(FeeReceive.STATUS_PAY_SUCCESS);
		Page<FeeReceive> page = feeReceiveService.findPage(new Page<FeeReceive>(request, response), feeReceive); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("feeReceive", feeReceive);
		return "modules/business/feeReceiveList";
	}

	@RequiresPermissions("business:feeReceive:view")
	@RequestMapping(value = "form")
	public String form(FeeReceive feeReceive, Model model) {
		model.addAttribute("feeReceive", feeReceive);
		return "modules/business/feeReceiveForm";
	}

	@RequiresPermissions("business:feeReceive:edit")
	@RequestMapping(value = "save")
	public String save(FeeReceive feeReceive, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, feeReceive)){
			return form(feeReceive, model);
		}
		feeReceiveService.save(feeReceive);
		addMessage(redirectAttributes, "保存实收台账成功");
		
		redirectAttributes.addFlashAttribute("feeReceive", UserUtils.getCache("feeReceive"));
		return "redirect:"+Global.getAdminPath()+"/business/feeReceive/?repage";
	}
	
	@RequiresPermissions("business:feeReceive:edit")
	@RequestMapping(value = "delete")
	public String delete(FeeReceive feeReceive, RedirectAttributes redirectAttributes) {
		feeReceiveService.delete(feeReceive);
		addMessage(redirectAttributes, "删除实收台账成功");
		
		redirectAttributes.addFlashAttribute("feeReceive", UserUtils.getCache("feeReceive"));
		return "redirect:"+Global.getAdminPath()+"/business/feeReceive/?repage";
	}

	@RequiresPermissions("business:feeReceive:edit")
	@RequestMapping("undoReceive")
	public String undoReceive(FeeReceive feeReceive, RedirectAttributes redirectAttributes){
		FeeCharge feeCharge = feeChargeService.get(feeReceive.getFeeChargeId());
		feeCharge.setStatus(FeeCharge.STATUS_UNFINISH);
		feeChargeService.save(feeCharge);
		feeReceiveService.delete(feeReceive);
		addMessage(redirectAttributes, "撤销实收台账成功");
		redirectAttributes.addFlashAttribute("feeReceive", UserUtils.getCache("feeReceive"));
		return "redirect:"+Global.getAdminPath()+"/business/feeReceive/?repage";
	}

}